package edu.nf.hotel.roominfodao.impl;

import edu.nf.hotel.roominfo.RoomInfo;
import edu.nf.hotel.roominfo.all.RoomTypeInfo;
import edu.nf.hotel.roominfodao.RoomTypeInfoDao;
import edu.nf.hotel.util.ConnUtils;
import org.nf.db.util.SqlExecutor;
import org.nf.db.util.result.BeanListHandler;
import org.nf.db.util.result.ColumnHandler;

import java.util.ArrayList;
import java.util.List;

/**
 * @Date 2023-06-30
 * @Author 带土
 */
public class RoomTypeInfoImpl implements RoomTypeInfoDao {
    /**
     * 查询全部客房类型信息+客房信息实体类
     * @return
     */
    @Override
    public List<RoomTypeInfo> getAllRoomTypeInfo() {
        SqlExecutor executor = new SqlExecutor(ConnUtils.getConnection());
        BeanListHandler<RoomTypeInfo> handler = new BeanListHandler<>(RoomTypeInfo.class);
        String sql = "select rtype_id,roomtype,roomtype_image,room_id,rt_id,ht_id,room_no,room_name,room_image," +
                "room_facilities,room_price,remark from roomtype_info inner join room_info on rtype_id=rt_id group by " +
                "rtype_id,roomtype,roomtype_image,room_id,rt_id,ht_id,room_no,room_name,room_image,room_facilities,room_price,remark";
        List<RoomTypeInfo> list = executor.executeQuery(sql, handler);
        list.forEach(roomTypeInfo -> System.out.println(roomTypeInfo.getRoomType()));
        return list;
    }

    /**
     * 根据客房类型查询全部客房类型信息+客房信息实体类
     * @param roomType
     * @return
     */
    @Override
    public List<RoomTypeInfo> getTypeRoomTypeInfo(String roomType) {
        SqlExecutor executor = new SqlExecutor(ConnUtils.getConnection());
        String sql = "select rtype_id,roomtype,roomtype_image,room_id,rt_id,ht_id,room_no,room_name,room_image,room_facilities," +
                "room_price,remark from roomtype_info left join room_info on rtype_id=rt_id where roomtype=? group by" +
                " rtype_id,roomtype,roomtype_image,room_id,rt_id,ht_id,room_no,room_name,room_image,room_facilities,room_price,remark;";
        BeanListHandler<RoomTypeInfo> handler = new BeanListHandler<>(RoomTypeInfo.class);
        List<RoomTypeInfo> list = executor.executeQuery(sql, handler, roomType);
        list.forEach(roomTypeInfo -> System.out.println(roomTypeInfo.getRoomTypeImage()));
        return list;
    }

    /**
     * 根据客房名称查询全部客房类型信息+客房信息实体类
     * @param roomName
     * @return
     */
    @Override
    public List<RoomTypeInfo> getNameRoomTypeInfo(String roomName) {
        SqlExecutor executor = new SqlExecutor(ConnUtils.getConnection());
        String sql = "select rtype_id,roomtype,roomtype_image,room_id,rt_id,ht_id,room_no,room_name,room_image,room_facilities," +
                "room_price,remark from roomtype_info left join room_info on rtype_id=rt_id where room_name=? group by" +
                " rtype_id,roomtype,roomtype_image,room_id,rt_id,ht_id,room_no,room_name,room_image,room_facilities,room_price,remark";
        BeanListHandler<RoomTypeInfo> handler = new BeanListHandler<>(RoomTypeInfo.class);
        List<RoomTypeInfo> list = executor.executeQuery(sql, handler, roomName);
        list.forEach(roomTypeInfo -> System.out.println(roomTypeInfo.getRoomImage()));
        return list;
    }

    /**
     * 查询分页信息
     * @param roomInfo
     * @param numberOf
     * @param limit
     * @return
     */
    @Override
    public List<RoomTypeInfo> listInformation(RoomInfo roomInfo, int numberOf, int limit) {
        SqlExecutor executor = new SqlExecutor(ConnUtils.getConnection());

        List<Object> params = new ArrayList<>();

        StringBuilder sql = new  StringBuilder("select room_id,rt_id,ht_id,room_no,room_name,room_image,room_facilities,room_price,remark from room_info");

        if (roomInfo != null && roomInfo.getRoomName() != null && !"".equals(roomInfo.getRoomName().trim())){
            sql.append(" where room_name like ? ");
            params.add("%" + roomInfo.getRoomName() + "%");
        }
        sql.append(" limit ?,?");

        params.add(numberOf);
        params.add(limit);

        BeanListHandler<RoomTypeInfo> handler = new BeanListHandler<>(RoomTypeInfo.class);

        return executor.executeQuery(sql.toString(),handler,params.toArray());
    }

    /**
     * 查询全部数据
     * @return
     */
    @Override
    public Long count() {
        SqlExecutor executor = new SqlExecutor(ConnUtils.getConnection());
        String sql = "select count(*) from  room_info";
        ColumnHandler<Long> handler = new ColumnHandler<>(1);
        return executor.executeQuery(sql,handler);
    }

}
